package com.AEWMS.Service;

import com.AEWMS.Dao.Student;
import com.AEWMS.Pojo.StudentInfoView;
import com.AEWMS.Utlis.DbCon;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 * @author lhs
 * @create 2021/2/22 16:16
 */
public class StudentService {

    private Connection conn = null ;
    private PreparedStatement pstmt = null ;

    public StudentService() {
        DbCon dbcon;
        try {
            dbcon = new DbCon();
            this.conn = dbcon.getConnection();
        } catch (Exception e) {
            System.out.println("StudentService 连接数据库失败");
            e.printStackTrace();
        }
    }

    public void closeCon(){
        try {
            this.conn.close();
        } catch (Exception e) {
            System.out.println("数据库连接关闭");
            e.printStackTrace();
        }
    }

    /**
     * 查找所有学生
     * @return 所有学生信息
     */
    public ArrayList<StudentInfoView> findAllStudent(){
        ArrayList<StudentInfoView> allStudentList = new ArrayList();//升级版数组，返回所有学生对象
        String sql = "SELECT * FROM student_info_view";
        try {
            this.pstmt = conn.prepareStatement(sql);
            ResultSet rs = this.pstmt.executeQuery();
            while (rs.next()){
                StudentInfoView s = new StudentInfoView();//按行查找，为false时查询结束
                s.setStudentNumber(rs.getString(1));
                s.setStudentRealName(rs.getString(2));
                s.setStudentSex(rs.getString(3));
                s.setCollegeName(rs.getString(4));
                s.setGradeId(rs.getString(5));
                s.setMajorName(rs.getString(6));
                s.setClassName(rs.getString(7));
                s.setStudentTel(rs.getString(8));

                allStudentList.add(s);//将查找到的行（每个学生对象）添加到数组
            }
        } catch (Exception e) {
            System.out.println("findAllStudent 失败");
            System.out.println(e);
        }
        return allStudentList;//输出包含所有学生对象的数组
    }

    /**
     * 查找学生
     * @param id 通过id
     * @return 返回一个学生对象
     */
    public StudentInfoView findStuById(String id){
        StudentInfoView s = new StudentInfoView();
        String sql = "SELECT * FROM student_info_view WHERE student_number = ?";
        try {
            this.pstmt = conn.prepareStatement(sql);//将sql语句放到数据库的准备对象
            this.pstmt.setString(1,id);//将传入id值替换问号放在SQL
            ResultSet rs = this.pstmt.executeQuery();//执行查询返回结果集
            while (rs.next()){
                s.setStudentNumber(rs.getString(1));
                s.setStudentRealName(rs.getString(2));
                s.setStudentSex(rs.getString(3));
                s.setCollegeName(rs.getString(4));
                s.setGradeId(rs.getString(5));
                s.setMajorName(rs.getString(6));
                s.setClassName(rs.getString(7));
                s.setStudentTel(rs.getString(8));
           }
        }
        catch (Exception e) {
            System.out.println("findStuById 失败");
            System.out.println(e);
        }
        return s;
    }

    /**
     * 查找学生
     * @param id 通过id
     * @return 返回一个学生对象
     */
    public Student findAllStuNotView(String id){
        Student s = new Student();
        String sql = "SELECT * FROM student_info_view WHERE student_number = ?";
        try {
            this.pstmt = conn.prepareStatement(sql);//将sql语句放到数据库的准备对象
            this.pstmt.setString(1,id);//将传入id值替换问号放在SQL
            ResultSet rs = this.pstmt.executeQuery();//执行查询返回结果集
            while (rs.next()){
                s.setStudentNumber(rs.getString(1));
                s.setStudentRealName(rs.getString(2));
                s.setStudentSex(rs.getString(4));
                s.setClassId(rs.getString(5));
                s.setStudentTel(rs.getString(6));
                s.setType(rs.getString(7));

            }
        }
        catch (Exception e) {
            System.out.println("findAllStuNotView 失败");
            System.out.println(e);
        }
        return s;
    }

    /**
     * 查找学生
     * @param id 通过id
     * @return 返回一个学生对象
     */
    public Student findStuNotViewById(String id){
        Student s = new Student();
        String sql = "SELECT * FROM student_info WHERE student_number = ?";
        try {
            this.pstmt = conn.prepareStatement(sql);//将sql语句放到数据库的准备对象
            this.pstmt.setString(1,id);//将传入id值替换问号放在SQL
            ResultSet rs = this.pstmt.executeQuery();//执行查询返回结果集
            while (rs.next()){
                s.setStudentNumber(rs.getString(1));
                s.setStudentRealName(rs.getString(2));
                s.setPassword(rs.getString(3));
                s.setStudentSex(rs.getString(4));
                s.setClassId(rs.getString(5));
                s.setStudentTel(rs.getString(6));
                s.setType(rs.getString(7));
            }
        }
        catch (Exception e) {
            System.out.println("findStuNotView 失败");
            System.out.println(e);
        }
        return s;
    }
    /**
     * 添加学生
     * @return 一个学生对象
     */
    public boolean insertStu(Student s){
        boolean flag = false;
        String sql = "INSERT INTO student_info VALUES(?,?,?,?,?,?,?)";
        try {
            this.pstmt = conn.prepareStatement(sql);//sql语句已经在这个准备对象里，有问号就要用准备对象
            this.pstmt.setString(1,s.getStudentNumber());
            this.pstmt.setString(2,s.getStudentRealName());
            this.pstmt.setString(3,s.getPassword());
            this.pstmt.setString(4,s.getStudentSex());
            this.pstmt.setString(5,s.getClassId());
            this.pstmt.setString(6,s.getStudentTel());
            this.pstmt.setString(7,s.getType());

            int rs = this.pstmt.executeUpdate();//这里不用再传sql
            if ( rs == 1 ) {
                flag = true;
            }
        }
        catch (Exception e){
            System.out.println("insertStu defeat");
            System.out.println(e);
        }
        return flag;
    }

    /**
     * 修改一个学生
     * @param id,password
     * @return 是否成功
     */
    public boolean updateStu(String id,String password) {
        boolean flag = false;
        String sql = "UPDATE student_info SET password = ? WHERE student_number = ?";//这里的字段名要与数据库的一致
        try {
            this.pstmt = conn.prepareStatement(sql);
            this.pstmt.setString(1,password);
            this.pstmt.setString(2,id);//准备对象里用的是参数索引值，参数和问号的顺序对应，因为是将传进来的内容放在准备对象里
            int rs = this.pstmt.executeUpdate();
            if ( rs == 1 ) {
                flag = true;
            }
        } catch (Exception e) {
            System.out.println("updateAStu 失败");
            System.out.println(e);
        }
        return flag;

    }

    /**
     * 删除一个学生
     * @param id 学生id
     * @return 是否成功
     */
    public boolean deleteStuById(String id){
        boolean flag = false;
        String sql = "DELETE FROM student_info WHERE student_number = ?";//这里的字段名要与数据库的一致
        try {
            this.pstmt = conn.prepareStatement(sql);
            this.pstmt.setString(1,id);//这里的名字与Javabean的一致
            int rs = this.pstmt.executeUpdate();
            if ( rs == 1 ) {
                flag = true;
            }
        }
        catch (Exception e){
            System.out.println("deleteUserById 失败");
            System.out.println(e);
        }
        return flag;
    }

}
